package com.wmlive.hhvideo.heihei.recordvideo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dyuiapi.api.DyUIAPI;
import com.dyuiapi.api.PublishVideoBaseFragment;
import com.wmlive.hhvideo.heihei.beans.subject.TopicInfo;
import com.wmlive.hhvideo.heihei.recordvideo.UserShorVideoInfoExtraInfo;
import com.wmlive.hhvideo.heihei.subject.SubjectSearchActivity;

import cn.wmlive.hhvideo.R;

import static android.app.Activity.RESULT_OK;

/**
 * 自定义视频发布信息填写fragment
 */
public class PublishVideoFragmentImpl extends PublishVideoBaseFragment {
    final static int REQUEST_VIDEO_CODE = DyUIAPI.REQUEST_CODE_BASE + 1;
    private static final int MAX_NUM = 55;
    private EditText mEditText;
    private TextView mTextView;
    private Boolean mbHasTopic = false;
    private ImageView mbtnDelTopic;
    private LinearLayout mLlTopic;
    private UserShorVideoInfoExtraInfo userShorVideoInfoExtraInfo;

    public PublishVideoFragmentImpl() {
        super();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_publish, container, false);

        mTextView = (TextView) rootView.findViewById(R.id.edit_text_huati);
        mbtnDelTopic = (ImageView) rootView.findViewById(R.id.btn_edit_text_huati_del);
        mEditText = (EditText) rootView.findViewById(R.id.edit_text_miaoshu);
        mLlTopic = (LinearLayout) rootView.findViewById(R.id.ll_topic_edit);
        mLlTopic.setOnClickListener(onClickListener);
        mEditText.addTextChangedListener(watcher);
        mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override
            public boolean onEditorAction(TextView v,int actionId,KeyEvent event){
                return(event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        //mEditText.requestFocus();//设置焦点
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        setTitle(getShortVideoInfo().getTitle());
        userShorVideoInfoExtraInfo = (UserShorVideoInfoExtraInfo) getShortVideoInfo().getExtraInfo();
        if (userShorVideoInfoExtraInfo != null && userShorVideoInfoExtraInfo.getTopicInfo() != null) {
            setTopic(userShorVideoInfoExtraInfo.getTopicInfo().getMstrTopic());
            mbHasTopic = true;
        } else {
            mbHasTopic = false;
        }

        if (mbHasTopic) {
            mbtnDelTopic.setVisibility(View.VISIBLE);
        } else {
            mbtnDelTopic.setVisibility(View.GONE);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_VIDEO_CODE) {
            if (resultCode == RESULT_OK) {
                TopicInfo topicInfo = data.getParcelableExtra(TopicInfo.INTENT_EXTRA_KEY_NAME);
                if (topicInfo != null) {
                    mbtnDelTopic.setVisibility(View.VISIBLE);
                    mbHasTopic = true;
                    setTopic(topicInfo.getMstrTopic());
                    getShortVideoInfo().setTopic(topicInfo.getMstrTopic());
                    if (userShorVideoInfoExtraInfo == null) {
                        userShorVideoInfoExtraInfo = new UserShorVideoInfoExtraInfo();
                    }
                    userShorVideoInfoExtraInfo.setTopicInfo(topicInfo);
                    getShortVideoInfo().setExtraInfo(userShorVideoInfoExtraInfo);//可自定义的扩展信息
                } else {
                    mbtnDelTopic.setVisibility(View.GONE);
                    mbHasTopic = false;
                }
            }
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mbHasTopic) {
                //清除话题信息
                setTopic(null);
                getShortVideoInfo().setTopic("");
                if (userShorVideoInfoExtraInfo != null) {
                    userShorVideoInfoExtraInfo.setTopicInfo(null);
                    getShortVideoInfo().setExtraInfo(userShorVideoInfoExtraInfo);
                }
                mbHasTopic = false;
                mbtnDelTopic.setVisibility(View.GONE);
            } else {
                startActivityForResult(new Intent(PublishVideoFragmentImpl.this.getActivity(), SubjectSearchActivity.class), REQUEST_VIDEO_CODE);
            }
        }
    };

    TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            getShortVideoInfo().setTitle(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() > MAX_NUM) {
                s.delete(MAX_NUM, s.length());
                onToast("已超过文字上限");
                //Toast.makeText(PublishVideoFragmentImpl.this.getActivity(), "已超过文字上限", Toast.LENGTH_SHORT).show();
            }
        }
    };

    /**
     * 设置发布时的标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        if (null != mEditText) {
            mEditText.setText(title);
        }
    }

    /**
     * 设置发布时主题
     *
     * @param topic 主题
     */
    public void setTopic(String topic) {
        if (mTextView != null) {
            if (TextUtils.isEmpty(topic)) {
                topic = "添加爆点";
            }
            mTextView.setText(topic);
        }
    }

}
